package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    // dubbo消费者
    @Reference
    UserService userService;


    // 从SpringSecurity中获取用户信息
    @RequestMapping(value = "/findUsername")
    public Result findUsername(){
        try {
            // 只要是认证通过，使用SecurityContextHolder来存放用户信息
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 成功
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    // 新增检查项（返回 public Result(boolean flag, String message)）
    @RequestMapping(value = "/add")
    public Result add(@RequestBody com.itheima.health.pojo.User user, @RequestParam Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
            // 成功
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }

    }

    // 根据id，删除检查项
    @RequestMapping(value = "/delete")
    public Result delete(Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (RuntimeException e) { // 遇到运行时异常，表示检查项和检查组的关联表中存在数据
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    // 使用id查询检查项
    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody com.itheima.health.pojo.User user,Integer[] roleIds) {
        try {
            userService.edit(user,roleIds);
            return new Result(true, MessageConstant.UPDATE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_USER_FAIL);
        }
    }

    // 使用id查询检查项
    @RequestMapping(value = "/findById")
    public Result findById(Integer id) {
        try {
            /*Map<String,Object> map= userService.findById(id);*/
            com.itheima.health.pojo.User user = userService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //根据用户id查询其所拥有的的角色
    @RequestMapping(value = "/findRolesListByUserId")
    public List<Integer> findRolesListByUserId(Integer id) {
        List<Integer> list = userService.findRolesListByUserId(id);
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    // 查询检查项（分页）
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        // 使用查询条件，当前页，当前页最多显示的记录数，查询封装PageResult（总记录数，每页显示的数据List）
        return userService.findPage(queryPageBean);
    }

}
